package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;
import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.MyInvitationCode;
import com.chain.BrotherExchange.utils.Androids;

/**
 * 我的邀请码
 */
public class MyInvitationCodeViewModel extends AndroidViewModel {

    private MutableLiveData<Resource<MyInvitationCode.DataBeanX.DataBean>> mMyInvitationLiveData;

    public MyInvitationCodeViewModel(@NonNull Application application) {
        super(application);
        mMyInvitationLiveData = new MutableLiveData<>();
    }

    public MutableLiveData<Resource<MyInvitationCode.DataBeanX.DataBean>> getMyInvitationData() {
        int uid = Androids.LocalConfig.getUid();
        String token = Androids.LocalConfig.getToken();
        mMyInvitationLiveData.setValue(Resource.loading());

        DataRepository.get().myInvitationCode(token, uid, result -> {
            MyInvitationCode.DataBeanX resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                mMyInvitationLiveData.setValue(Resource.success(resultData.getDataX()));
            } else if (result.isNotNetwork()) {
                mMyInvitationLiveData.setValue(Resource.notNetwork());
            } else {
                mMyInvitationLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mMyInvitationLiveData;
    }
}
